package com.springboot.cloud.sysadmin.organization.rest;

import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.entity.form.DictDataForm;
import com.springboot.cloud.sysadmin.organization.entity.form.DictDataQueryForm;
import com.springboot.cloud.sysadmin.organization.entity.form.DictTypeForm;
import com.springboot.cloud.sysadmin.organization.entity.form.DictTypeQueryForm;
import com.springboot.cloud.sysadmin.organization.entity.param.DictDataQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.param.DictTypeQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.DictData;
import com.springboot.cloud.sysadmin.organization.entity.po.DictType;
import com.springboot.cloud.sysadmin.organization.service.IDictDataService;
import com.springboot.cloud.sysadmin.organization.service.IDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@RestController
@RequestMapping("/dict/type")
@Api(tags = "字典类型")
@Slf4j

public class DictTypeController {

    @Autowired
    private   IDictTypeService iDictTypeService;

    @ApiOperation(value = "新增字典类型", notes = "新增一个字典类型")
    @ApiImplicitParam(name = "dictTypeForm", value = "新增字典类型form表单", required = true, dataType = "DictTypeForm")
    @PostMapping
    public Result add(@Valid @RequestBody DictTypeForm dictTypeForm) {
        log.debug("dictTypeForm:{}", dictTypeForm);
        if (iDictTypeService.add(dictTypeForm.toPo(DictType.class))) {
            return Result.success("添加字典类型成功");
        } else {
            return Result.error("不能有重复的字典类型名称");
        }
    }

    @ApiOperation(value = "删除字典类型")
    @DeleteMapping
    public Result delete(@Valid @RequestBody Set<String> ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error("没选中要删除的字典类型");
        } else if (iDictTypeService.delete(ids)) {
            return Result.success("删除字典类型成功");
        }
        return Result.error("没选中要删除的字典类型");

    }

    @ApiOperation(value = "修改字典类型", notes = "修改指定字典类型信息")
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody DictTypeForm dictTypeForm) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        DictType dictType = dictTypeForm.toPo(DictType.class);
        dictType.setId(id);
        if (iDictTypeService.update(dictType)) {
            return Result.success("修改字典类型成功");
        }
        return Result.error("更新失败");

    }

    @ApiOperation(value = "获取字典类型", notes = "获取指定字典类型信息")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        if (iDictTypeService.get(id)) {
            return Result.success(iDictTypeService.get(id));
        }
        return Result.error("查找不到对应值");

    }

    @ApiOperation(value = "搜索字典类型", notes = "根据条件查询字典类型信息")
    @PostMapping(value = "/conditions")
    public Result search(@Valid @RequestBody DictTypeQueryForm dictTypeQueryForm) {
        if (dictTypeQueryForm.getCreatedTime() != null && !dictTypeQueryForm.getCreatedTime().isEmpty()) {
            dictTypeQueryForm.setCreatedTimeStart(dictTypeQueryForm.getCreatedTime().get(0));
            dictTypeQueryForm.setCreatedTimeEnd(dictTypeQueryForm.getCreatedTime().get(1));
        }
        log.debug("search with dictTypeQueryForm:{}", dictTypeQueryForm);
        return Result.success(iDictTypeService.query(dictTypeQueryForm.getPage(), dictTypeQueryForm.toParam(DictTypeQueryParam.class)));
    }

    @ApiOperation(value = "获取所有字典类型", notes = "获取所有字典类型")
    @GetMapping(value = "/all")
    public Result get() {
        return Result.success(iDictTypeService.getAll());
    }

}
